<?php
declare (strict_types = 1);

namespace app\index\controller;
use think\facade\View;
use think\facade\Db;
use think\captcha\facade\Captcha;

use app\common\model\Order as OrderModel;
use app\common\model\Lists as ListsModel;
use app\common\model\Users as UsersModel;

use app\admin\validate\Order as validateOrder;

class Order extends Base
{
    protected $middleware = [
        'CountTime' => ['only' 		=> ['order_confirm'] ],
    ];

    public function addOrder(){
        $param = input('param.');

        $validate = new validateOrder();
        $result = $validate->scene('create')->batch(false)->check($param);
        if(!$result){
            return dyajaxReturn(0,$validate->getError());
        }
        $OrderModel = new OrderModel();
        $param['act'] = 'add';
        $res = $OrderModel->handle($param);

        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }



    public function order_confirm(){
        if(IS_POST){
            $param = input('param.');

            $validate = new validateOrder();
            $result = $validate->scene('edit')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $OrderModel = new OrderModel();
            $ord_one = $OrderModel -> where('id','=',$param['id'])->find();
            if( $ord_one['add_time'] + 1800 <= time() ){
                return dyajaxReturn(0,'订单支付时间已超时');
            }

            if($ord_one['price'] == 0){
                $param['pay_status'] = 3;
                $param['pay_time'] = time();
                $res = $OrderModel->handle($param);
                return dyajaxReturn(2,$res['msg'],$res['data']);
            }

            $res = $OrderModel->handle($param);
            return dyajaxReturn($res['code'],$res['msg'],$res['data']);
        }else {
            $param = input('param.');
            /*$lists_id = $param['lists_id'];
            $ListsModel = new ListsModel();
            $info = $ListsModel->where('id', '=', $lists_id)->find();*/

            $order_id = $param['order_id'];
            $OrderModel = new OrderModel();
            $info = $OrderModel->where('id','=',$order_id)->find();
            if($info['pay_status'] != 1){
                $this->redirect(url('users/user_center_order'));
            }

            $ListsModel = new ListsModel();
            $lists_one = $ListsModel->where('id', '=', $info['lists_id'])->find();
            $info['duration'] = $lists_one['duration'];
            $info['effective_date'] = $lists_one['effective_date'];
            $info['sign_up_num'] = $lists_one['sign_up_num'];
            $info['add_time'] = $info['add_time'] + 1800;

            View::assign('info', $info);
            View::assign('order_id', $order_id);
            return View::fetch('index/order_confirm');
        }
    }


    //支付成功页面
    public function successpage(){


        return View::fetch('index/successpage');
    }

}
